package com.jian.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jian.bus.pojo.Customer;
import com.jian.bus.pojo.Goods;
import com.jian.bus.pojo.Sales;
import com.jian.bus.pojo.Salesback;
import com.jian.bus.service.CustomerService;
import com.jian.bus.service.GoodsService;
import com.jian.bus.service.SalesService;
import com.jian.bus.service.SalesbackService;
import com.jian.bus.vo.SalesVo;
import com.jian.bus.vo.SalesbackVo;
import com.jian.sys.common.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 剑
 * @since 2020-01-26
 */
@RestController
@RequestMapping("/salesback")
public class SalesbackController {
    @Autowired
    CustomerService customerService;
    @Autowired
    GoodsService goodsService;
    @Autowired
    SalesbackService salesbackService;
    /**
     * 查询所有和模糊查询
     */
    @RequestMapping("/loadAllSalesback")
    public DataGridView loadAllSalesback(SalesbackVo salesbackVo){

        IPage<Salesback> page = new Page<>(salesbackVo.getPage(),salesbackVo.getLimit());
        QueryWrapper<Salesback> wrapper = new QueryWrapper<>();
        wrapper.eq(salesbackVo.getCustomerid()!=null&&salesbackVo.getCustomerid()!=0,"customerid",salesbackVo.getCustomerid());
        wrapper.eq(salesbackVo.getGoodsid()!=null&&salesbackVo.getGoodsid()!=0,"goodsid",salesbackVo.getGoodsid());
        wrapper.like(StringUtils.isNotBlank(salesbackVo.getOperateperson()),"operateperson",salesbackVo.getOperateperson());
        wrapper.like(StringUtils.isNotBlank(salesbackVo.getRemark()),"remark",salesbackVo.getRemark());
        wrapper.ge(salesbackVo.getStartTime()!=null,"salesbacktime",salesbackVo.getStartTime());
        wrapper.le(salesbackVo.getEndTime()!=null,"salesbacktime",salesbackVo.getEndTime());
        wrapper.orderByDesc("salesbacktime");
        this.salesbackService.page(page,wrapper);
        List<Salesback> list = page.getRecords();
        //通过商品id设置商品名称 商品规格
        for (Salesback salesback : list) {
            Goods goods = this.goodsService.getById(salesback.getGoodsid());
            if(null!=goods){
                salesback.setGoodsname(goods.getGoodsname());
                salesback.setSize(goods.getSize());
            }
        }
        //通过客户id设置客户名称
        for (Salesback salesback : list) {
            Customer customer=this.customerService.getById(salesback.getCustomerid());
            if(null!=customer){
                salesback.setCustomername(customer.getCustomername());
            }
        }
        return new DataGridView(page.getTotal(),list);
    }

}

